<?php
namespace app\applet\model;
use think\Db;
use think\Model;

class IntentionalHouse extends Model {
    protected  $table = 'intentional_house';
    public function __construct($data = [])
    {
//        parent::__construct($data);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCount($type) {
        $data = Db::name($this->table)
            ->field('house_id, count(house_id)')
            ->group('house_id')
            ->having('status=1')
            ->having('type=' . $type)
            ->select();
        return $data;
    }

    /**
     * @param $type
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneCount($type, $id) {
        $data = Db::name($this->table)
            ->where('status=1')
            ->where('type=' . $type)
            ->where('house_id=' . $id)
            ->count();
        return $data;
    }

    /**
     * @param $keeper_id
     * @param $page
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function HouseList($keeper_id, $page) {
        $where=[];
        $where['status']=1;
        if($keeper_id!==''){
            $where['keeper_id']=$keeper_id;
        }
        $count=Db::name($this->table)
            ->where($where)
            ->count();
        $data=Db::name($this->table)
            ->where($where)
            ->page($page, 10)
            ->field('id, keeper_id, house_id, create_time, type')
            ->order("type asc")
            ->select();
        $date['data']=$data;
        $date['count']=$count;

        return $date;
    }

    /**
     * 唯一性
     * @param $house_id
     * @param $type
     * @param $keeper_id
     * @return int|string
     * @throws \think\Exception
     */
    public function uniqueness($house_id, $type, $keeper_id) {
        $where['status'] = 1;
        $where['house_id'] = $house_id;
        $where['type'] = $type;
        $where['keeper_id'] = $keeper_id;
        $count = Db::name($this->table)
            ->where($where)
            ->count();
        return $count;
    }

}